home *** CD-ROM | disk | FTP | other *** search
- Path: calvin.stemnet.nf.ca!jdeeley
- From: jdeeley@calvin.stemnet.nf.ca (J.Deeley)
- Newsgroups: comp.lang.c
- Subject: Re: Understanding pointers
- Date: Sat, 13 Apr 1996 11:41:09 -0400
- Organization: I need to be organized?
- Message-ID: <4kohlc$k62@coranto.ucs.mun.ca>
- References: <Barron.K.Herrington.21.01DF12CE@tek.com>
- NNTP-Posting-Host: calvin.stemnet.nf.ca
- Mime-Version: 1.0
- Content-Type: text/plain; charset=ISO-8859-1
- Content-Transfer-Encoding: 8bit
- X-Newsreader: MacSOUP 1.0d7
-
- Barron Herrington <Barron.K.Herrington@tek.com> wrote:
-
- > I need help understanding pointers, I understand variables and have the
- > concept of pointers, but I can't seem to get it into code. Any quick
- > explanation or easy projects or easy short program examples would be greatly
- > appreciated.
-
-
- Not to rejoice in your misery, but I am glad that I am not the only one
- having this problem. I reread the chapter on pointers about 5 times.
- Like you, I understand the _concept_ very well, but have a bit of
- trouble implementing it! I just bought another C text book but I just
- got it and have been reviewing other stuff in it (interesting how
- different books approach the same problems!) and haven't got to pointers
- in it, yet. I am hoping it will make it even clearer.
-
- Here is an easy program which may help make how to use a simple pointer
- a little clearer:
-
- #include <stdio.h>
-
-
- /***********************/
- /* Function Prototypes */
- /***********************/
- void SquareIt( int number, int *squarePtr );
-
-
- int main( void )
- {
- int square;
-
- SquareIt( 5, &square );
-
- printf( "5 squared is %d.\n", square );
-
- return 0;
- }
-
-
- void SquareIt( int number, int *squarePtr )
- {
- *squarePtr = number * number;
- }
-
-
- My (limited because I am new to this too, so take this with a grain of
- salt!) understanding is that you need to use the address of operator (&)
- and pointers (*) in order to be able to change the information the
- address contains, or to share the data between functions, since there
- are only three ways to do that.
-
- One way to share data between functions is by using global variables
- (memory hungry)
- another is to use the return function (limited by only being able to
- return one peice of data)
- and the third way is by passing parameters by using values (can't give
- returns except as above) or addresses (as in the above code example)
- which you can then change and therefore return data with by using a
- pointer (bingo!) When you look at it that way, you can see why pointers
- are so essential to C! In a way, you have no choice but use them.
-
- Or, as this paragraph from my text puts it:
-
- Since passing parameters by value is a one-way operation, there╣s no way
- to get data back from the called function. Why would you ever want to?
- Several reasons. You might write a function that takes an employee
- number as a parameter. You might want that function to return the
- employee╣s salary in another parameter. How about a function that turns
- yards into meters? You could pass the number of yards as a value
- parameter, but how would you get back the number of meters? Passing a
- parameter by address (instead of by value) solves this problem. If you
- pass the address of a variable, the receiving function can use the *
- operator to change the value of the original variable.
-
- quote ends.
-
- In the code case above, we need to be able to pass back to the main
- function the info from mulitplying number and number. We could do this
- by using return in this example, but that would hardly let us learn
- about pointers!
-
- So, in main, the parameter list of SquareIt passes an int (5) and the
- address of the variable "square" to the function SquareIt. But since
- that won't get the info back to main, we use *squarePtr in the function
- parameter list to be able to change the information that &square has in
- it.
-
- Pointers (like *squarePtr) are special because they HOLD the ADDRESS of
- another variable! In other words, *squarePtr is holding the ADDRESS of
- square. Which means it can change what square has in it! All from the
- comfort of another function. <lazy pointers> *grin*
-
- One thing that always messes me up is why do we need both & and *? It
- would be a lot simpler if we only used one of those symbols to do the
- same thing. So I think of it this way; the & operator is the real
- accessor to the variables address. It acts like an open door. "Here I
- am" it says, "change me!"
-
- The pointer just literally points at the variable, like a finger. It
- sort of says (imagine a finger called *squarePtr pointing at the
- variable address of square in main) "That's where I am going to put this
- information!!" and thanks to the address of's (&) open door policy,
- *squarePtr can.
-
- Why use *squarePtr instead of square in SquareIt?
- It would not do any good to just use the variable address "square" in
- the function SquareIt, because as soon as SquareIt exits, the data in a
- regular address is lost, no longer there. Has no effect on main. We need
- to change it permanantly so that main can have it. That's why we use a
- pointer. So now when main goes to printf "5 squared is" square now holds
- the info that number times number produced, thanks to using the address
- of operator and the pointer.
-
-
- It seems to me from observation that a general rule is that you -pass-
- the pointers to the called function using the & (address of) operator
- when they are in a parameter list (opening the door), but when you are
- declaring functions (such as SquareIt in the above example) you use the
- * operator. (pointing to where to put the info in the open door) I think
- as long as we follow this general rule, we may be safe for now? <grin>
-
- I think another thing that had me confused for a while is the fact that
- you can rename the pointers. At first I thought they should have the
- same name as what they pointed to. For instance, in the above example,
- &square can become *squarePtr. But when you figure that all they do is
- POINT to the address they are changing it makes more sense.
-
- Anyway, what I did was use this general code format (as above) and write
- a whole bunch of little programs that returned addition, subtraction,
- etc. instead of square numbers. it made me feel a little better about
- the whole thing ;) So maybe you could try something similar. Just write
- some little programs
-
- I hope this long ramble has helped. If anyone wants to comment or add to
- our understanding, feel free. I need all the help I can get. I have
- already said that I am a newbie to, so I would also appreciate not being
- flamed if my attempt to help is not perfect.
-
-
- JD
-
- ---------------------
- Caution...Newbie Programmer on Board!
- --
-
-